import VueRouter, { RouteConfig } from 'vue-router'
import { Store as MgStore } from 'vuex'
import Vue, { CreateElement } from 'vue'
import vueCustomUse from './plugins/vueCustomUse'
import router from './router'
import store from './store'
import { Component } from 'vue-property-decorator'
// 默认多语言不启用，如需启用请取消下面的注释以及app变量内的注释，并且为了类型正确请使用this.$mg_t
// import i18n from './i18n'
import './statics/styles/quasar.styl'
import './statics/global.css'

Component.registerHooks([
    'beforeRouteEnter',
    'beforeRouteLeave'
])

const isPortal = Vue.prototype.isPortal

Vue.config.productionTip = false
Vue.config.devtools = process.env.NODE_ENV === 'development'

const name = process.env.APP_SCOPE_NAME

let plugins: any[] = [{ default: vueCustomUse }]

if (!isPortal) {
    let subPlugins: any[] = [
        import('./plugins/vueMixin'),
        import('./plugins/vueUse'),
        import('./plugins/httpJs')
        // import('./plugins/websocketJs') // 不用websocket，可以注释此行
    ]
    Promise.all([
        ...subPlugins,
        import('./App.vue')
    ]).then(res => {
        const App = res[res.length - 1].default
        const pluginsArr: any[] = res.slice(0, res.length - 1)
        const app = {
            el: '#vue',
            // i18n,
            router: <VueRouter>router,
            store: <MgStore<any>>store,
            render: (h: CreateElement) => h(App)
        }
        plugins = [...plugins, ...pluginsArr]
        plugins.forEach(plugin => {
            plugin.default({ app, router: router, store: store, Vue })
        })
        /* eslint-disable no-new */
        new Vue(app)
    })
} else {
    Promise.all([
        import('micro-frontends-server')
    ]).then(res => {
        const CreateRegister = res[0].CreateRegister
        const register = new CreateRegister({
            name: (<string>name),
            poolName: window.poolName
        })
        const that = register.registerModule(store).addRoutes(router as RouteConfig[])
        plugins.forEach(plugin => plugin.default({ router: that.pool.router, store: that.pool.store, Vue }))
    })
}
